<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2021-04-10 09:17:14
 */

namespace app\mobmall_manage\controller;
use app\common\controller\Backend;

class Config extends Backend {

	private $config_space = 'mall';

	function initialize() {
		parent::initialize();
		$this->saveAndGetConfigIdentify('', '商城配置', '', 'config');

		$this->configDao = model('common/Config');
	}

	public function index() {
		$info = $this->configDao->getConfigsBySpace($this->config_space);
		$this->assign('info', $info);

		//获取微信公众号列表
		$weixinAccountDao = model('weixin_manage/WeixinAccount');
		list($weixinAccountList) = $weixinAccountDao->getAllList();
		$this->assign('weixinAccountList', $weixinAccountList);

		return $this->fetch();
	}

	public function doSet() {
		$data = input('post.data/a', '', 'pwEscape');

		$info = $this->configDao->getConfigsBySpace($this->config_space);

		//支付方式
		$allow_pay_method = input('post.allow_pay_method/a', '', 'pwEscape');
		$data['allow_pay_method'] = !empty($allow_pay_method) ? intval(array_sum($allow_pay_method)) : 0;
		
		foreach ($data as $key => $value) {
			$checkInfo = $this->configDao->getConfigByNameAndSpace($key, $this->config_space);
			$sqlData = [
				'config_value'	=> $value,
			];
			//print_r($checkInfo);
			if ($checkInfo) {
				$this->configDao->updateConfigByNameAndSpace($key, $this->config_space, $sqlData);
			} else {
				$sqlData['config_space'] = $this->config_space;
				$sqlData['config_name'] = $key;
				$sqlData['description'] = '';
				//print_r($sqlData);exit;
				$this->configDao->insert($sqlData);
			}
		}
		$this->clearCache();

		$this->success('保存成功');
	}

	private function clearCache() {
		$sys_identify = 'mall_config';
		cache($sys_identify, NULL); //删除缓存数据
	}
	
}